package com.ywjt.platformbusiness.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ywjt.platformbusiness.EventBusBean;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.store.fragment.BusinessCenterFragment;
import com.ywjt.platformbusiness.store.fragment.GoodsManagerFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MyStoreActivity  extends BaseActivity implements View.OnClickListener {

    LinearLayout llBusinessCenter,llGoodsMana;
    private List<Fragment> fragmentList;
    private MainAdapter adapter;
    private ViewPager viewPager;
    private TextView tvCenter,tvMana;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyStoreActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llBusinessCenter:
                viewPager.setCurrentItem(0);
                tvCenter.setTextColor(getResources().getColor(R.color.red));
                tvMana.setTextColor(getResources().getColor(R.color.gray9));
                break;
            case R.id.llGoodsMana:
                viewPager.setCurrentItem(1);
                tvMana.setTextColor(getResources().getColor(R.color.red));
                tvCenter.setTextColor(getResources().getColor(R.color.gray9));
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
      setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.my_store_activity;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        llBusinessCenter=findViewById(R.id.llBusinessCenter);
        llBusinessCenter.setOnClickListener(this::onClick);
        llGoodsMana=findViewById(R.id.llGoodsMana);
        viewPager=findViewById(R.id.viewPager);
        tvCenter=findViewById(R.id.tvCenter);
        tvMana=findViewById(R.id.tvMana);
        llGoodsMana.setOnClickListener(this::onClick);

        fragmentList=new ArrayList<>();
        fragmentList.add(BusinessCenterFragment.newInstance());
        fragmentList.add(GoodsManagerFragment.newInstance());
        adapter=new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        tvCenter.setTextColor(getResources().getColor(R.color.red));
                        tvMana.setTextColor(getResources().getColor(R.color.gray9));
                        break;
                    case 1:
                        tvMana.setTextColor(getResources().getColor(R.color.red));
                        tvCenter.setTextColor(getResources().getColor(R.color.gray9));
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    @Subscribe()
    public void change(EventBusBean eventBus){
        switch (eventBus.getCode()){
            case 100:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentList.get(position).toString();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().unregister(this);
        }
    }
}
